package com.ruoyi.device.contorller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.device.domain.Device;
import com.ruoyi.device.service.DeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备设施管理Controller
 *
 * @author zjh
 * @date 2022-04-28
 */
@Controller
@RequestMapping("/device")
public class DeviceController extends BaseController
{
    private String prefix = "device";

    @Autowired
    private DeviceService deviceService;

    @RequiresPermissions("business:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询设备设施管理列表
     */
    @RequiresPermissions("business:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Device device)
    {
        startPage();
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        Map<String,Object> map = new HashMap();
        map.put("companyId",companyId);
        if (device.getDeviceName()!=null&&!"".equals(device.getDeviceName())){
            map.put("deviceName",device.getDeviceName());
        }
        List<Device> list = deviceService.selectDeviceByComId(map);
        return getDataTable(list);
    }


    /**
     * 新增设备设施管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备设施管理
     */
    @RequiresPermissions("business:device:add")
    @Log(title = "设备设施管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Device device)
    {
        Long companyId = ShiroUtils.getSysUser().getCompanyId();
        device.setCompanyId(companyId);
        device.setCreateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(deviceService.insertDevice(device));
    }

    /**
     * 修改设备设施管理
     */
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        Device device = deviceService.selectDeviceById(deviceId);
        mmap.put("device", device);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备设施管理
     */
    @RequiresPermissions("business:device:edit")
    @Log(title = "设备设施管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Device device)
    {
        device.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备设施管理
     */
    @RequiresPermissions("business:device:remove")
    @Log(title = "设备设施管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }
}
